package com.fau.cm.action.index;


import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCall;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  中心话务管理Action
 * @author yangzj
 * @datetime 2014年10月14日,上午10:39:10
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CallManageCenterAction extends PublicAction implements ModelDriven<MedicalCall>{
	
	private static final long serialVersionUID = 6349279192012513543L;
	
	private MedicalCall call = new MedicalCall();
	
	/**
	 * @desc 页面初始化加载Tabs
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月24日,下午10:03:41
	 */
	@Action(value = "call_manage_center_init", results = {@Result(location = "call_manage_center_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 跳转话务任务管理--未分配页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月24日,下午10:45:23
	 */
	@Action(value = "call_manage_center_una_init", results = {@Result(location = "call_manage_center_una_init.jsp")})
	public String unallocated(){
		return SUCCESS;
	}

	/**
	 * @desc 分页查询话务任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月15日,下午4:46:43
	 */
	@Action(value = "call_manage_center_una_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getCallCenterManagerService().findCallByPage(call, admin, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	
	/**
	 * @desc 回收任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月29日,下午5:48:01
	 */
	@Action(value = "call_manage_center_una_recover")
	public String recover() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId())) {
			this.baseAllService.getCallCenterManagerService().recoverUnaCall(call.getId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量修改类别
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月23日,下午2:07:46
	 */
	@Action(value = "call_manage_center_una_cat")
	public String editCategory(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId()) && StringUtils.isNotBlank(call.getCategory())){
			this.baseAllService.getCallCenterManagerService().editCallCategory(call.getId(), call.getCategory());
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	
	/**
	 * @desc 批量修改任务状态
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月25日,下午4:59:10
	 */
	@Action(value = "call_manage_center_una_sta")
	public String editStatus() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId()) && StringUtils.isNotBlank(call.getPocessingStatus())) {
			this.baseAllService.getCallCenterManagerService().editCallStatus(call.getId(), call.getPocessingStatus());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除话务任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月19日,下午7:25:29
	 */
	@Action(value = "call_manage_center_una_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId())){
			this.baseAllService.getCallCenterManagerService().delCall(call);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 导出全部-已分配
	 * @return null
	 * @author liy
	 * @datetime 2014年10月31日,上午11:55:09
	 */
	@Action(value = "call_manage_center_assigned_export_all")
	public String assignedExportAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心已分配话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getCallCenterManagerService().assignedExportAll(admin,this.call);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出当前-已分配
	 * @return null
	 * @author liy
	 * @datetime 2015年1月5日,下午1:49:23
	 */
	@Action(value = "call_manage_center_assigned_export_cur")
	public String unaExportCurrent(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心已分配话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			if(StringUtils.isNotBlank(call.getId())){
				Workbook workbook = this.baseAllService.getCallCenterManagerService().assignedExportCurrent(call.getId());
				workbook.write(toClient);
			}
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @desc 查询中心所有话务任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月19日,下午1:44:17
	 */
	@Action(value = "call_manage_center_all_init", results = {@Result(location = "call_manage_center_all_init.jsp")})
	public String allCallInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 查询中心所有
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月19日,下午1:44:53
	 */
	@Action(value = "call_manage_center_all_view")
	public String allCallView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getCallManagerService().findAllCenterCallByPage(call, map, admin);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 导出当前(中心)
	 * @return null
	 * @author liy
	 * @datetime 2015年1月4日,下午4:38:55
	 */
	@Action(value = "call_manage_center_exports_cur")
	public String exportsCurrent(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request, "所有话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			if(StringUtils.isNotBlank(call.getId())){
				Workbook workbook = this.baseAllService.getCallManagerService().callManageAllExportCurrent(call.getId());
				workbook.write(toClient);
			}
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出所有(中心)
	 * @return null
	 * @author liy
	 * @datetime 2015年1月4日,下午4:38:55
	 */
	@Action(value = "call_manage_center_exports_all")
	public String exportsCurrentAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request, "所有话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getCallManagerService().callManageCenterExportAll(call, admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalCall getModel() {
		return call;
	}
	

}
